from rest_framework import serializers
from course.models import CourseDirectionModel,CourseCategoryModel,CourseModel,TeacherModel,CourseChapterModel
# 课程方向的序列化器
class CourseDirectionModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = CourseDirectionModel
        fields = ["id","name"]
# 课程分类的序列化器
class CourseCategoryModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = CourseCategoryModel
        fields = ["id","name"]

class CourseModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = CourseModel
        fields = "__all__"

class TeacherModelSerializer(serializers.ModelSerializer):
    class Meta():
        model = TeacherModel
        fields = ['id','name','get_role_display','title','signature','avatar','brief']


class CourseRetrieveSerializer(serializers.ModelSerializer):
    direction_name = serializers.CharField(source='direction.name',read_only=True)
    category_name = serializers.CharField(source='category.name',read_only=True)
    # category_name = serializers.SerializerMethodField()
    #
    # def get_category_name(self):
    teacher = TeacherModelSerializer()
    class Meta():
        model = CourseModel
        fields = ['name','course_cover','course_video','course_type','get_level_display',
                  'status','description','pub_date','period','attachment_path','attachment_link',
                  'students','lessons','pub_lessons','price','credit','direction_name','category_name','direction','category','teacher','discount']


class CourseChapterSerializer(serializers.ModelSerializer):
    class Meta():
        model = CourseChapterModel
        fields = ['id', 'orders', 'name', 'summary', 'pub_date', 'course', 'get_lesson_list']
